﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using EBA.Data;
using EBA.Contracts;
using EBA.Helpers;
using EBA.Localization;

namespace EBA.Logging
{
    class XmlFileLogQuery : IQuery<ILog>
    {
        string LogFile
        {
            get;
            set;
        }

        public XmlFileLogQuery(string path)
        {
            LogFile = path;
        }

        private IEnumerable<ILog> GetLogs()
        {
            using (var stream = File.OpenRead(this.LogFile))
            {
                using (var reader = new StreamReader(stream))
                {
                    while (reader.Peek() > -1)
                    {
                        var buffer = reader.ReadLine();
                        if (string.IsNullOrWhiteSpace(buffer))
                        {
                            yield return null;
                        }
                        else
                        {
                            var log = XmlFileLogSerializer.Deserialize(buffer);
                            yield return log;
                        }
                    }
                }
            }
        }

        public IQueryable<ILog> AsQuerybale
        {
            get
            {
                return GetLogs().AsQueryable();
            }
        }

        public void WithWhere(string predicate)
        {
            throw new NotImplementedException();
        }
    }
}
